const { Router } = require('express')
const express = require('express')
//路由容器
const interview = express.Router()
const axios = require('axios')
const qs = require('qs')
const jwt = require("jsonwebtoken")
//面试者管理页面
interview.get('/', (req, res) => {
    const {id}=req.query
    if (id == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:'http://1.117.92.175:8081/user/findByStudentId',
            method:'GET',
            params: {
                studentId:id
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.render('admin/interview',{result:data.data.object,userInfo:jwt.decode(req.session.token)})
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
// 查看面试者信息
interview.get('/getState', (req, res) => {
    const {id}=req.query
    if (id == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:'http://1.117.92.175:8081/user/findByStudentId',
            method:'GET',
            params: {
                studentId:id
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send({data:data.data.object})
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
//遍历
interview.get('/findByInterview', (req, res) => {
        axios({
            url: 'http://1.117.92.175:8081/quession/findByInterview',
            method: 'GET',
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
})
//查找用户id
interview.get('/findId', (req, res) => {
    const {name} = req.query
        axios({
            url: 'http://1.117.92.175:8081/admin/findUserById',
            method: 'GET',
            params:{
                id:name
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send({data:data.data.object})
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
})


//打分
interview.get('/grade', (req, res) => {
    const {attitude,communicate,compression,heart,interviewersId,state,strain,study,understand,userid} = req.query
    let urls;
    if(state=="待面试"){
        urls="http://1.117.92.175:8081/addCountsOne"
    }else{
        urls="http://1.117.92.175:8081/addCountsTwo"
    }
    if (attitude == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:urls,
            method:'POST',
            params: {
                attitude :attitude,
                communicate:communicate,
                compression :compression,
                heart :heart,
                interviewerId:interviewersId,
                strain :strain,
                study:study,
                understand :understand,
                userid :userid
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
//添加评论
interview.get('/addCommen', (req, res) => {
    const {desc,interviewersId,state,userid} = req.query
    let urls;
    if(state=="待面试"||state=="待一面"){
        urls="http://1.117.92.175:8081/addCommentOne"
    }else{
        urls="http://1.117.92.175:8081/addCommentTwo"
    }
    if (desc == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:urls,
            method:'POST',
            params: {
                content:desc,
                interviewerId:interviewersId,
                userid :userid
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
// 遍历评论
interview.get('/findCommen', (req, res) => {
    const {interviewersId,state} = req.query
    let urls;
    if(state=="待二面"){
        urls="http://1.117.92.175:8081/findByInterviewerAndState"
    }else{
        urls="http://1.117.92.175:8081/findComments"
    }
    if (interviewersId == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:urls,
            method:'GET',
            params: {
                interviewerId:interviewersId,
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
// 遍历分数
interview.get('/findScore', (req, res) => {
    const {interviewersId,value} = req.query
    let urls;
    urls="http://1.117.92.175:8081/findAccountOne"
    if (interviewersId == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:urls,
            method:'GET',
            params: {
                interviewerId:interviewersId,
                value:value
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
// 遍历平均分
interview.get('/findAverageScore', (req, res) => {
    const {interviewersId,state} = req.query
    let urls,value;
    urls ="http://1.117.92.175:8081/find"
    if (interviewersId == undefined) {
        return res.status(400).render('admin/error', {msg:'参数错误'})
    } else {
        axios({
            url:urls,
            method:'GET',
            params: {
                interviewerId:interviewersId,
                value:state,
            },
            headers:{
                token:req.session.token?req.session.token:"",
            },
        })
        .then(data => {
            res.send(data.data)
        })
        .catch(err => {
            // res.send(err)
            return res.status(400).render('admin/error',{msg:"请求出错了",err:err})
        })
    }
})
module.exports=interview;
